<?php
declare (strict_types = 1);

namespace app\controller\backend;

use app\model\InnerChart;
use app\validate\InnerChartValidate;
use think\exception\ValidateException;


class InnerChartController extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function index(InnerChart $innerChart): \think\response\Json
    {

        $limit = 10;
        $limitParam = (int)input('limit');
        if($limitParam){
            $limit = $limitParam;
        }
        $where = [
            ['seller_id' ,'=', $this->admin['seller_id']],
        ];
        $keyword = input('keyword');
        if($keyword){
            $where[] = ['keyword','like','%'.$keyword.'%'];
        }
        $innerChartList = $innerChart->getInnerChartList($where,$limit);
        return json(pageReturn($innerChartList));
    }

    /**
     * 保存新建的资源
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function save(InnerChart $innerChart): \think\response\Json
    {
        if(request()->isPost()){
            $param = input('post.');
            $param['seller_id'] = $this->admin['seller_id'];

            // 数据验证
            try{
                validate(InnerChartValidate::class)->scene('save')->check($param);
            }catch(ValidateException $e){
                return jsonReturn(-1, $e->getError());
            }

            $res = $innerChart -> addInnerChart($param);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }

    /**
     * 显示指定的资源
     *
     * @return \think\response\Json
     * @throws \app\exception\ModelException
     * @throws \app\exception\ModelEmptyException
     */
    public function read(InnerChart $innerChart): \think\response\Json
    {

        $id = (int)input('id');
        if(!$id){
            return jsonReturn(-1,'内链ID不能为空');
        }
        $where = [
            'id' => $id,
            'seller_id' => $this->admin['seller_id']
        ];
        $res = $innerChart->getInnerChart($where);
        return json($res);

    }

    /**
     * 保存更新的资源

     * @return \think\response\Json
     * @throws \app\exception\ModelException
     */
    public function update(InnerChart $innerChart): \think\response\Json
    {
        if(request()->isPost()){
            $param = input('post.');
            try {
                validate(InnerChartValidate::class)->scene('update')->check($param);
            } catch (ValidateException $e) {
                return jsonReturn(-1, $e->getError());
            }
            $where = [
                'id' => $param['id'],
                'seller_id' => $this->admin['seller_id'],
            ];
            $res = $innerChart -> updateInnerChart($where,$param);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }

    /**
     * 删除指定资源
     *
     * @throws \app\exception\ModelException
     */
    public function delete(InnerChart $innerChart): \think\response\Json
    {
        if(request()->isPost()){
            $id = (int)input('id');
            if(!$id){
                return jsonReturn(-1,'内链ID不能为空');
            }
            $where = [
                'id' => $id,
                'seller_id' => $this->admin['seller_id']
            ];
            $res = $innerChart->delInnerChart($where);
            return json($res);
        }
        return jsonReturn(-3,'请求方法错误');
    }
}
